<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="banner.css">

</head>

<body>
    <div class="mybanner">
        <ul class="hot">
            <li>
                <a href=""> <img src="../images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="../images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="../images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="../images/banner4.jpg" alt=""> </a>
            </li>
        </ul>
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <ol class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"> </li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
        </ol>

    </div>
    <script>
        window.onload = function() {
            var width = 730;
            var i = 0;
            var hot = document.querySelector('.hot');
            var temp = hot.firstElementChild.cloneNode(true);
            var mybanner = document.querySelector('.mybanner');
            hot.appendChild(temp);
            var imgChange = () => {
                i++;
                hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                hot.style.transition = '0.5s';
                if (i == hot.childElementCount - 1) {
                    i = 0;
                    setTimeout(() => {
                        hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                        hot.style.transition = 'none';
                    }, 500);
                }
                setCircles();
            }

            var imgChanges = () => {
                if (i == 0) {
                    i = hot.childElementCount - 1;
                    hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                    hot.style.transition = 'none';
                }
                i--;
                setTimeout(() => {
                    hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                    hot.style.transition = '0.5s';
                }, 0);
                setCircles();
            }
            var next = document.querySelector('.arrow .next');
            next.onclick = imgChange;
            var prev = document.querySelector('.prev');
            prev.onclick = imgChanges;
            //定时轮播
            var timer = setInterval(imgChange, 1000);
            mybanner.onmouseover = () => {
                clearInterval(timer);
            }
            mybanner.onmouseout = () => {
                timer = setInterval(imgChange, 1000);
            }
            var setCircles = () => {
                var circles = document.querySelectorAll('.circle');
                // for (var index = 0; index < circles.length; index++) {
                //     if (index === i) {
                //         circles[index].classList.add('active');
                //     } else {
                //         circles[index].classList.remove('active');
                //     }
                // }

                circles.forEach(item => {
                    var n = item.getAttribute('data-n');
                    if (n == i) {
                        item.classList.add('active');
                    } else {
                        item.classList.remove('active');
                    }
                })
            }
            var circleList = document.querySelector('.circle-list');
            circleList.onclick = (e) => {
                if (e.target.nodeName == 'LI') {
                    i = Number(e.target.getAttribute('data-n'))
                    hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                    hot.style.transition = '0.5s ease';
                    setCircles()
                }
            }
        }
    </script>
</body>

</html>